import 'dart:async';

import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';

class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
        actions: <Widget>[
          IconSource("Stream","18"),
        ],
      ),
      body:StreamDemo() ,
    );
  }
}
class StreamDemo extends StatefulWidget {
  @override
  _StreamDemoState createState() => _StreamDemoState();
}

class _StreamDemoState extends State<StreamDemo> {

  StreamController<String> streamController;
  StreamSubscription subscription;
  String _subLogs="...";
  @override
  void initState() {
    super.initState();
    streamController=StreamController<String>.broadcast();
    subscription=streamController.stream.listen(onData,onDone: _onDone);
  }
  void _add() async{
      String value=await getData();
    streamController.sink.add(value);
  }
  Future<String> getData() async{
    await Future.delayed(Duration(seconds: 1));
    return "hello!!";
  }
  void _onDone(){
    setState(() {
      _subLogs=_subLogs+"done\n";
    });
  }
  void onData(value){
    setState(() {
      _subLogs=_subLogs+value+"\n";
    });
  }
  void onPause(){
    subscription.pause();
    setState(() {
      _subLogs=_subLogs+"pause\n";
    });
  }
  void onResume(){
    subscription.resume();
    setState(() {
      _subLogs=_subLogs+"resume\n";
    });
  }
  void onCancle(){
    subscription.cancel();
    setState(() {
      _subLogs=_subLogs+"cancle\n";
    });
  }
  @override
  Widget build(BuildContext context) {
    debugPrint("build widgte");
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          //Text(_result),
          StreamBuilder<String>(
            stream: streamController.stream,
            initialData: "....",
            builder: (context,snapshot){
              return Text("${snapshot.data}");
            },
          ),
          Wrap(
            spacing: 3.0,
            children: <Widget>[
              OutlineButton(
                child: Text("add"),
                onPressed: _add,
              ),
              OutlineButton(
                child: Text("pause"),
                onPressed: onPause,
              ),
              OutlineButton(
                child: Text("resume"),
                onPressed: onResume,
              ),
              OutlineButton(
                child: Text("cancle"),
                onPressed: onCancle,
              ),
            ],
          ),
          ListTile(
            title: Text("log："),
            subtitle: Text(_subLogs),
          ),
        ],
      ),
    );
  }
  @override
  void dispose() {
    super.dispose();
    streamController.close();
    debugPrint("demo18 disposed!");
  }
}


